﻿using UnityEngine;
using System.Collections;

public class CardScroll : MonoBehaviour {

	Vector3[] aPos = new Vector3[5] { new Vector3(-120, 0, 0), new Vector3(-70, 0, 0), new Vector3(0, 0, 0), new Vector3(70, 0, 0), new Vector3(120, 0, 0) };
	Vector3[] aScale = new Vector3[5] { new Vector3(0.7f, 0.7f, 0), new Vector3(0.8f, 0.8f, 0), new Vector3(1, 1, 0), new Vector3(0.8f, 0.8f, 0), new Vector3(0.7f, 0.7f, 0) };

	Transform[] img = new Transform[5];
	Transform trans;

	Vector3 downPos;
	bool downFlag = false;
	int[] aNext = new int[5];
	int[] aCenciR = new int[5] {1, 3, 5, 4, 2 };
	int[] aCenciL = new int[5] { 2, 4, 5, 3, 1 };
	int[] aCurPos = new int[5] { 0, 1, 2, 3, 4 };
	void Start () {
		trans = transform;

		trans.FindChild("Button").SetSiblingIndex(6);
		for (int i = 0; i < 5; i++)
		{
			img[i] = trans.FindChild("Button" + i);
			img[i].localPosition = aPos[i];
			img[i].localScale = aScale[i];
			img[i].SetSiblingIndex(aCenciR[i]);
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0))
		{
			downFlag = true;
			downPos = Input.mousePosition;
		}
		float deltaDis = (Input.mousePosition - downPos).x;
		if (downFlag)
		{

			//print(Input.mousePosition - downPos);
			if (Mathf.Abs((Input.mousePosition - downPos).x) / 100f > 1)
			{
				for (int j = 0; j < 5; j++)
				{
					if (deltaDis > 0)
					{
						aCurPos[j] = aCurPos[j] + 1 == 5 ? 0 : aCurPos[j] + 1;
					}
					if (deltaDis < 0)
					{
						aCurPos[j] = aCurPos[j] - 1 == -1 ? 4 : aCurPos[j] - 1;
					}
					downPos = Input.mousePosition;
				}
			}
			for (int i = 0; i < 5; i++)
			{
				if (deltaDis > 0)
				{
					aNext[i] = (aCurPos[i] + 1) == 5 ? 0 : (aCurPos[i] + 1);
					img[i].SetSiblingIndex(aCenciR[aNext[i]]);
					//if (aNext[i] == 2)
					//	img[i].SetAsLastSibling();

					//if (aCurPos[i] == 4)
					//	img[i].SetAsFirstSibling();

					img[i].localPosition = Vector3.Lerp(aPos[aCurPos[i]], aPos[aNext[i]], (Input.mousePosition - downPos).x / 100f);
					img[i].localScale = Vector3.Lerp(aScale[aCurPos[i]], aScale[aNext[i]], (Input.mousePosition - downPos).x / 100f);


					if (Mathf.Abs((Input.mousePosition - downPos).x) / 100f > 1)
					{
						//aCurPos[i] = aCurPos[i] + 1 == 5 ? 0 : aCurPos[i] + 1;
					}
				} 
				if (deltaDis < 0)
				{
					aNext[i] = (aCurPos[i] - 1) == -1 ? 4 : (aCurPos[i] - 1);
					img[i].SetSiblingIndex(aCenciL[aNext[i]]);

					//if (aNext[i] == 2)
					//	img[i].SetAsLastSibling();

					//if (aCurPos[i] == 0)
					//	img[i].SetAsFirstSibling();

					img[i].localPosition = Vector3.Lerp(aPos[aCurPos[i]], aPos[aNext[i]], (downPos - Input.mousePosition).x / 100f);
					img[i].localScale = Vector3.Lerp(aScale[aCurPos[i]], aScale[aNext[i]], (downPos - Input.mousePosition).x / 100f);

					if (Mathf.Abs((Input.mousePosition - downPos).x) / 100f > 1)
					{
						//aCurPos[i] = aCurPos[i] - 1 == -1 ? 4 : aCurPos[i] - 1;
					}
				}
			}
		}
		if (Input.GetMouseButtonUp(0))
		{
			if (Mathf.Abs((Input.mousePosition - downPos).x) / 100f > 0.5)
			{
				for (int j = 0; j < 5; j++)
				{
					if (deltaDis > 0)
					{
						aCurPos[j] = aCurPos[j] + 1 == 5 ? 0 : aCurPos[j] + 1;
					}
					if (deltaDis < 0)
					{
						aCurPos[j] = aCurPos[j] - 1 == -1 ? 4 : aCurPos[j] - 1;
					}
					downPos = Input.mousePosition;
				}
			}
			for (int i = 0; i < 5; i++)
			{
				img[i].localPosition = aPos[aCurPos[i]];
				img[i].localScale = aScale[aCurPos[i]];
				//img[i].SetSiblingIndex(aCenci[aCurPos[i]]);

				if (deltaDis > 0)
				{
					img[i].SetSiblingIndex(aCenciR[aCurPos[i]]);
					//if (aCurPos[i] == 4)
					//	img[i].SetSiblingIndex(0);
				}
				if (deltaDis < 0)
				{
					img[i].SetSiblingIndex(aCenciL[aCurPos[i]]);
					//if (aCurPos[i] == 0)
					//	img[i].SetSiblingIndex(0);
				}
			}
			downFlag = false;
		}


			
	}
}
